package com.cyf.mq.producer;

import com.cyf.mq.constant.MqConstant;
import com.cyf.mq.pojo.HeroDTO;
import com.cyf.mq.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import java.util.Arrays;
import java.util.List;

/**
 * @author chenyifan
 * @create 2025-02-18 16:00
 */
@Slf4j
public class BatchProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer(MqConstant.MESSAGE_PRODUCER_GROUP_A);
        producer.setNamesrvAddr(MqConstant.NAME_SERVER_ADDR);
        producer.start();

        HeroDTO heroDTO1 = new HeroDTO("jialuo1", "伽罗1");
        HeroDTO heroDTO2 = new HeroDTO("jialuo2", "伽罗2");
        HeroDTO heroDTO3 = new HeroDTO("jialuo3", "伽罗3");
        HeroDTO heroDTO4 = new HeroDTO("jialuo4", "伽罗4");

        List<Message> messages = Arrays.asList(
                new Message(MqConstant.MESSAGE_TOPIC_King, JacksonUtil.toJsonString(heroDTO1).getBytes()),
                new Message(MqConstant.MESSAGE_TOPIC_King, JacksonUtil.toJsonString(heroDTO2).getBytes()),
                new Message(MqConstant.MESSAGE_TOPIC_King, JacksonUtil.toJsonString(heroDTO3).getBytes()),
                new Message(MqConstant.MESSAGE_TOPIC_King, JacksonUtil.toJsonString(heroDTO4).getBytes())
        );
        SendResult sendResult = producer.send(messages);
        log.info("发送成功：{}", sendResult);
        producer.shutdown();
    }
}
